package com.youjiang.meeting.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youjiang.meeting.po.Redpack;
import com.youjiang.meeting.po.UserInfo;
import com.youjiang.meeting.service.ControlService;
import com.youjiang.meeting.service.ManagermentService;
import com.youjiang.meeting.utils.StringKeyValue;
import com.youjiang.meeting.utils.ToolUtils;

import net.sf.json.JSONObject;

/**
 * <p>
 * Title: ManagermentController
 * </p> 
 * <p>
 * Description:TODO
 * </p> 
 * <p>
 * Company: YouJiang
 * </p> 
 *
 * @author feng
 * @date 2016年12月3日 上午9:52:20
 * @version 1.0
 */
@Controller
@RequestMapping(value="/manage",produces=StringKeyValue.CONTROLLERPRODUCES)
public class ManagermentController {
	@Autowired
	private ManagermentService service;
	@Resource
	private ControlService controlService;
	@RequestMapping("/select/resource")
	@ResponseBody
	public String selectResource(HttpSession session,@RequestParam(defaultValue = "0")Integer level){
		String data="";
		if(level==1){
			data=service.selectGame();
		}
		if(level==2){
			data=service.selectProgram(session);
		}
		if(level==3){
			data=service.selectRedPack(session);
		}
		return data;
	}
	
	@RequestMapping(value="/del/resource")
	@ResponseBody
	public String delResource(HttpSession session,@RequestParam(defaultValue = "0")Integer id, @RequestParam(defaultValue = "0")Integer level) throws Exception{
		String data="";
		if(level==1){
			data = service.delGame(id);
		}
		if(level==2){
			data=service.delProgram(session,id);
		}
		if(level==3){
			data=service.delRedPack(session,id);
		}
		return data;
	}
	@RequestMapping("/add/game")
	@ResponseBody
	public String addGame(String name,String answer){
		String data=service.addGame(name, answer);
		return data;
	}
	@RequestMapping("/add/program")
	@ResponseBody
	public String addProgram(HttpSession session,String name,String principal){
		UserInfo info = (UserInfo) session.getAttribute("userInfo");
		JSONObject object = null;
		if(info==null){
			object = new JSONObject();
			return ToolUtils.json(object, "00", "用户登录超时,请重新登录");
		}
		String data=service.addProgram(info.getId(),name, principal);
		return data;
	}
	@RequestMapping("/add/redpack")
	@ResponseBody
	public String addRedPack(HttpSession session,Redpack pack){
		UserInfo info = (UserInfo) session.getAttribute("userInfo");
		JSONObject object = null;
		if(info==null){
			object = new JSONObject();
			object.put("code", 00);
			object.put("msg", "用户登录超时,请重新登录");
			return object.toString();
		}
		String data=service.addRedPack(info.getId(),pack);
		return data;
	}
	@RequestMapping("/login")
	public String login(String sign,HttpServletRequest request){
		String data=service.login(sign, request);
		return data;
	}
	@RequestMapping("/export")
	public void export(HttpServletResponse res){
		service.exportInfo(res);
	}
	@RequestMapping("/setinfo")
	@ResponseBody
	public String setInfo(String company,String actname,String remark){
		return controlService.setName(company, actname, remark);
	}
	@RequestMapping("/getinfo")
	@ResponseBody
	public String getInfo(){
		return controlService.getName();
	}
	@RequestMapping("/index")
	public String loginJSP(){
		return "login";
	}
	
}
